<template>
  <div class="register-container">
    <el-card header="添加用户" class="register-card" shadow="hover">
      <el-form @submit.native.prevent="register">
        <el-form-item label="用户名">
          <el-input v-model="model.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="model.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码">
          <el-input type="password" v-model="confirmPassword" placeholder="请再输入一次密码"></el-input>
        </el-form-item>
        <el-form-item>
          <div class="button">
            <el-button type="primary" native-type="submit">提交</el-button>
            <el-button type="success" @click="$router.push('/adminlist')">查看用户列表</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      model: {
        username: ''
      },
      confirmPassword: ''
    }
  },
  methods: {
    async register() {
      if (this.model.username.length > 0 && this.model.password === this.confirmPassword) {
        const res = await this.$http.post('/register', this.model)
        if (res.status === 200) {
          this.$message({
            message: '用户添加成功',
            type: 'success'
          })
          this.$router.push('/adminlist')
        }
      } else if(!this.model.username.length > 0){
        this.$message.error('请输入用户名')
      } else if(this.model.password !== this.confirmPassword) {
        this.$message.error('两次密码输入不一致')
      }
    }
  },
}
</script>

<style lang="scss">
.register-container {
  width: 25rem;
  font-weight: 700;
  margin: 6rem auto;
  .button {
    display: flex;
    justify-content: space-between;
  }
}
</style>
